<?php
/**
 * 后台模型
 * @author Administrator
 *
 */
class AdminModel extends Model {
	
	protected $tableName = "admin"; // 表名
	
	/**
	 * 按父ID查找菜单子项
	 *
	 * @param $parentid integer
	 *        	父菜单ID
	 * @param $with_self integer
	 *        	是否包括他自己
	 */
	public function admin_menu($parentid, $with_self = 0) {
		$parentid = intval ( $parentid );
		$Menu = D ( "Menu" );
		$where ['parentid'] = $parentid;
		$where ['display'] = 1;
		
		import ( "ORG.Util.Session" );
		
		// 角色权限处理
		$AdminInfo = $this->find ( Session::get ( "admin_uid" ) );
		if ($AdminInfo) {
			// 用户所属角色
			$AdminRolePriv = D ( "AdminRolePriv" );
			$AdminRolePrivInfo = $AdminRolePriv->where ( "roleid={$AdminInfo['roleid']}" )->find ();
			$data = unserialize ( $AdminRolePrivInfo ['data'] );
			$ids = join ( ',', $data );
		}
		
		// 如果是超级管理员角色,显示所有菜单
		if ($AdminInfo ['roleid'] == 1) {
			$result = $Menu->where ( "parentid={$parentid} AND display=1" )->select ();
		} else {
			$result = $Menu->where ( "parentid={$parentid} AND display=1 AND id in({$ids})" )->select ();
		}
		
		if ($with_self) {
			$result2 [] = $Menu->find ( $parentid );
			$result = array_merge ( $result2, $result );
		}
		return $result;
	}
}
?>